package com.lgmshare.k3.helper;

import android.app.Dialog;
import android.content.Context;

import com.lgmshare.component.utils.UIUtils;
import com.lgmshare.k3.helper.concurrency.Command;
import com.lgmshare.k3.helper.concurrency.CommandCallback;
import com.lgmshare.k3.ui.dialog.DialogUtils;

/**
 * 异步线程执行帮助类
 *
 * @author: lim
 * @version: V1.0
 * @description:
 * @datetime: 2015/9/7 12:29
 * @email: lgmshare@gmail.com
 */
public class ExecuteAsyncTaskHelper {

    /**
     * @param context
     * @param cmd     在异步线程中运行主题
     * @param <T>
     */
    public static <T> void execute(Context context, Command<T> cmd) {
        execute(context, null, cmd);
    }

    /**
     * @param context
     * @param message progressDialog提示信息
     * @param cmd     执行方法体-在异步线程中运行耗时的操作
     * @param <T>
     */
    public static <T> void execute(Context context, int message, Command<T> cmd) {
        execute(context, context.getString(message), cmd, null);
    }

    /**
     * @param context
     * @param message progressDialog提示信息
     * @param cmd     执行方法体-在异步线程中运行耗时的操作
     * @param <T>
     */
    public static <T> void execute(Context context, String message, Command<T> cmd) {
        execute(context, message, cmd, null);
    }

    /**
     * @param context
     * @param message  progressDialog提示信息
     * @param cmd      执行方法体-在异步线程中运行耗时的操作
     * @param callback 回调方法体-在UI线程中
     * @param <T>
     */
    public static <T> void execute(Context context, int message, Command<T> cmd, final CommandCallback<T> callback) {
        execute(context, context.getString(message), cmd, callback);
    }

    /**
     * @param context
     * @param message  progressDialog提示信息
     * @param cmd      执行方法体-在异步线程中运行耗时的操作
     * @param callback 回调方法体-在UI线程中
     * @param <T>
     */
    public static <T> void execute(Context context, String message, final Command<T> cmd, final CommandCallback<T> callback) {
        final Dialog progressDialog = DialogUtils.createProgressDialog(context, message);
        progressDialog.setCancelable(false);
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.getWindow().setDimAmount(0f);
        progressDialog.show();
        UIUtils.runOnAsyncThread(new Runnable() {
            @Override
            public void run() {
                cmd.start(new CommandCallback<T>() {
                    @Override
                    public void onResult(final T result) {
                        UIUtils.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                progressDialog.dismiss();
                                if (callback != null) {
                                    callback.onResult(result);
                                }
                            }
                        });
                    }

                    @Override
                    public void onError(final String error) {
                        UIUtils.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                progressDialog.dismiss();
                                if (callback != null) {
                                    callback.onError(error);
                                }
                            }
                        });
                    }
                });
            }
        });
    }

    /**
     * 不带progressDialog
     *
     * @param context
     * @param cmd      执行方法体-在异步线程中运行耗时的操作
     * @param callback 回调方法体-在UI线程中
     * @param <T>
     */
    public static <T> void executeNoProgressDlg(Context context, String message, final Command<T> cmd, final CommandCallback<T> callback) {
        UIUtils.runOnAsyncThread(new Runnable() {
            @Override
            public void run() {
                cmd.start(new CommandCallback<T>() {
                    @Override
                    public void onResult(final T result) {
                        UIUtils.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (callback != null) {
                                    callback.onResult(result);
                                }
                            }
                        });
                    }

                    @Override
                    public void onError(final String error) {
                        UIUtils.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                if (callback != null) {
                                    callback.onError(error);
                                }
                            }
                        });
                    }
                });
            }
        });
    }
}
